﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TGB.Core.Infrastructure;
using TGBTech.Core.Models.ChungTu;
using TGBTech.Core.Models.DanhMuc;
using TGBTech.Core.Models.Models.BaoCao;

namespace TGBTech.Core.Models.Services
{
    public class CanDoiPhatSinhService
    {

        public List<CanDoiPhatSinh> getListCDPS(DateTime ngaybatdau, DateTime ngayketthuc)
        {
            List<CanDoiPhatSinh> listRes; ;
            List<TaiKhoan> listTaiKhoan;
            decimal nocuoiky;
            decimal nodauky;
            using (TGBTechDb dbContext = new TGBTechDb())
            {
                listTaiKhoan = dbContext.TaiKhoan.ToList();
                listRes = getCDPSsFromTaiKhoans(listTaiKhoan);
                GenerateCDPSLevel(listRes);
                foreach (CanDoiPhatSinh CDPS in listRes)
                {
                    if (CDPS.Level > 0) continue;
                    var query = (from p in dbContext.Data
                                 where (p.TaiKhoanNoId == CDPS.MaTaiKhoan || p.TaiKhoanCoId == CDPS.MaTaiKhoan)
                                 && p.NgayCT <= ngayketthuc
                                 select p).ToList();
                    CDPS.NoDauKi = (from p in query
                                    where p.NgayCT < ngaybatdau && p.TaiKhoanNoId == CDPS.MaTaiKhoan
                                    select p.SoTien).ToList().Sum();
                    CDPS.NoPhatSinh = (from p in query
                                       where p.NgayCT >= ngaybatdau && p.NgayCT <= ngayketthuc && p.TaiKhoanNoId == CDPS.MaTaiKhoan
                                       select p.SoTien).ToList().Sum();
                    CDPS.CoDauKi = (from p in query
                                    where p.NgayCT < ngaybatdau && p.TaiKhoanCoId == CDPS.MaTaiKhoan
                                    select p.SoTien).ToList().Sum();
                    CDPS.CoPhatSinh = (from p in query
                                       where p.NgayCT >= ngaybatdau && p.NgayCT <= ngayketthuc && p.TaiKhoanCoId == CDPS.MaTaiKhoan
                                       select p.SoTien).ToList().Sum();
                    nodauky = CDPS.NoDauKi - CDPS.CoDauKi;
                    if (nodauky > 0)
                    {
                        CDPS.NoDauKi = nodauky;
                        CDPS.CoDauKi = 0;
                    }
                    else
                    {
                        CDPS.CoDauKi = -nodauky;
                        CDPS.NoDauKi = 0;
                    }
                    nocuoiky = CDPS.NoPhatSinh + CDPS.NoDauKi - CDPS.CoDauKi - CDPS.CoPhatSinh;
                    if (nocuoiky > 0)
                    {
                        CDPS.NoCuoiKi = nocuoiky;
                        CDPS.CoCuoiKi = 0;
                    }
                    else
                    {
                        CDPS.CoCuoiKi = -nocuoiky;
                        CDPS.NoCuoiKi = 0;
                    }
                }
                GenerateCDPS(listRes);
            }
            return listRes;
        }

        public List<CanDoiPhatSinh> getCDPSsFromTaiKhoans(List<TaiKhoan> taikhoans)
        {
            List<CanDoiPhatSinh> res = new List<CanDoiPhatSinh>();
            CanDoiPhatSinh CDPS;
            foreach (TaiKhoan taikhoan in taikhoans)
            {

                CDPS = new CanDoiPhatSinh()
                {
                    MaTaiKhoan = taikhoan.MaTaiKhoan,
                    TenTaiKhoan = taikhoan.Name
                };
                res.Add(CDPS);
            }
            return res;
        }

        public void GenerateCDPS(List<CanDoiPhatSinh> list)
        {
            decimal nocuoiky;
            int level = 0;
            while (level <= 4)
            {
                level++;
                foreach (var taikhoan in list)
                {
                    if (taikhoan.Level == level)
                    {
                        var query = (from e in list
                                     where taikhoan.MaTaiKhoan != e.MaTaiKhoan &&
                                             e.MaTaiKhoan.StartsWith(taikhoan.MaTaiKhoan, StringComparison.CurrentCulture) &&
                                             e.Level == level - 1
                                     select e);
                        taikhoan.NoDauKi = (from e in query select e.NoDauKi).ToList().Sum();
                        taikhoan.NoPhatSinh = (from e in query select e.NoPhatSinh).ToList().Sum();
                        taikhoan.CoDauKi = (from e in query select e.CoDauKi).ToList().Sum();
                        taikhoan.CoPhatSinh = (from e in query select e.CoPhatSinh).ToList().Sum();

                        nocuoiky = taikhoan.NoPhatSinh + taikhoan.NoDauKi - taikhoan.CoDauKi - taikhoan.CoPhatSinh;
                        if (nocuoiky > 0)
                        {
                            taikhoan.NoCuoiKi = nocuoiky;
                            taikhoan.CoCuoiKi = 0;
                        }
                        else
                        {
                            taikhoan.CoCuoiKi = -nocuoiky;
                            taikhoan.NoCuoiKi = 0;
                        }
                    }
                }
            }
        }

        public void GenerateCDPSLevel(List<CanDoiPhatSinh> list)
        {
            int level = -1;
            while (level <= 4)
            {
                level++;
                foreach (var taikhoan in list)
                {
                    var query = (from e in list
                                 where taikhoan.MaTaiKhoan != e.MaTaiKhoan &&
                                         e.MaTaiKhoan.StartsWith(taikhoan.MaTaiKhoan, StringComparison.CurrentCulture)
                                         && e.Level == level
                                 select e).Count();
                    if (query > 0) taikhoan.Level = level + 1;
                }
            }
        }
    }
}
